home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / viewing / polarView.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.6 KB  |  228 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* polarView.c - show how to combine modeling transformations to 
  19.  *        view objects as though they are encased in a glass ball
  20.  *
  21.  *    F1 key        - print help information
  22.  *    Left Arrow Key    - increment the azimuth angle 
  23.  *    Right Arrow Key    - decrement the azimuth angle
  24.  *    Up Arrow Key    - increment the incidence angle 
  25.  *    Down Arrow Key    - decrement the incidence angle 
  26.  *    R Key        - reset viewpoint
  27.  *    Escape key     - exit the program
  28.  */
  29. #include <GL/gl.h>
  30. #include <GL/glu.h>
  31. #include <GL/glut.h>
  32. #include <math.h>
  33. #include <stdio.h>    /* for printf */
  34.  
  35. /*  Function Prototypes  */
  36.  
  37. GLvoid  initgfx( GLvoid );
  38. GLvoid  drawScene( GLvoid );
  39. GLvoid  reshape( GLsizei, GLsizei );
  40. GLvoid  keyboard( GLubyte, GLint, GLint );
  41. GLvoid  specialkeys( GLint, GLint, GLint );
  42.  
  43. void resetView( GLvoid );
  44. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  45.  
  46. void printHelp( char * );
  47.  
  48. /* Global Definitions */
  49.  
  50. #define KEY_ESC    27    /* ascii value for the escape key */
  51.  
  52. /* Global Variables */
  53.  
  54. static char *progname;
  55.  
  56. static GLfloat        beamWidth = 2.0, beamHeight = 0.4, beamDepth = 1.0;
  57. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  58.  
  59. void
  60. main( int argc, char *argv[] )
  61. {
  62.     GLsizei width, height;
  63.  
  64.     glutInit( &argc, argv );
  65.  
  66.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  67.     height = glutGet( GLUT_SCREEN_HEIGHT );
  68.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  69.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  70.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  71.     glutCreateWindow( argv[0] );
  72.  
  73.     initgfx();
  74.  
  75.     glutKeyboardFunc( keyboard );
  76.     glutSpecialFunc( specialkeys );
  77.     glutReshapeFunc( reshape );
  78.     glutDisplayFunc( drawScene ); 
  79.  
  80.     progname = argv[0];
  81.     printHelp( argv[0] );
  82.  
  83.     glutMainLoop();
  84. }
  85.  
  86. void
  87. printHelp( char *progname )
  88. {
  89.     fprintf(stdout, "\n%s - combine modeling transformations to \n" 
  90.         "view objects as though encased in a glass ball\n\n"
  91.         "Axes: X - red, Y - green, Z - blue\n\n"
  92.         "F1 Key            - print Help information\n"
  93.         "Left Arrow Key        - increment the azimuth angle\n" 
  94.         "Right Arrow Key    - decrement the azimuth angle\n"
  95.         "Up Arrow Key        - increment the incidence angle\n" 
  96.         "Down Arrow Key        - decrement the incidence angle\n" 
  97.         "<R> Key        - reset viewpoint\n"
  98.         "Escape Key        - exit the program\n\n",
  99.         progname);
  100. }
  101.  
  102. GLvoid
  103. initgfx( GLvoid )
  104. {
  105.     GLfloat maxObjectSize;
  106.  
  107.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  108.     glShadeModel( GL_FLAT );
  109.     glEnable( GL_DEPTH_TEST );
  110.  
  111.     /* Maximum size of all the objects in your scene */
  112.     maxObjectSize = fsqrt( ((2*beamWidth) * (2*beamWidth)) +
  113.                 ((2*beamHeight) * (2*beamHeight)) +
  114.                 (beamDepth * beamDepth) );
  115.  
  116.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  117.     /* and determine the viewing distance (adjust for zooming) */
  118.     near = 1.0;
  119.     far = near + 8*maxObjectSize; 
  120.     resetView();
  121. }
  122.  
  123. GLvoid
  124. reshape( GLsizei width, GLsizei height )
  125. {
  126.     GLdouble            aspect;
  127.  
  128.     glViewport( 0, 0, width, height );
  129.  
  130.     aspect = (GLdouble) width / (GLdouble) height;
  131.  
  132.     glMatrixMode( GL_PROJECTION );
  133.     glLoadIdentity();
  134.     gluPerspective( 45.0, aspect, near, far );
  135.     glMatrixMode( GL_MODELVIEW );
  136. }
  137.  
  138. GLvoid 
  139. keyboard( GLubyte key, GLint x, GLint y )
  140. {
  141.     switch (key) {
  142.     case 'R':
  143.         resetView();
  144.         glutPostRedisplay();
  145.         break;
  146.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  147.         exit(0);
  148.     }
  149. }
  150.  
  151. GLvoid 
  152. specialkeys( GLint key, GLint x, GLint y )
  153. {
  154.     switch (key) {
  155.     case GLUT_KEY_F1:    /* print Help */
  156.         printHelp( progname );
  157.         break;
  158.  
  159.     case GLUT_KEY_LEFT:
  160.         azimAngle = fmodf( (azimAngle + 5.0), 360.0 );
  161.         glutPostRedisplay();
  162.          break;
  163.  
  164.     case GLUT_KEY_RIGHT:
  165.         azimAngle = fmodf( (azimAngle - 5.0), 360.0 );
  166.         glutPostRedisplay();
  167.         break;
  168.  
  169.     case GLUT_KEY_UP:    
  170.         incAngle = fmodf( (incAngle + 5.0), 360.0 );
  171.         glutPostRedisplay();
  172.         break;
  173.  
  174.     case GLUT_KEY_DOWN:    
  175.         incAngle = fmodf( (incAngle - 5.0), 360.0 );
  176.         glutPostRedisplay();
  177.         break;
  178.     }
  179. }
  180.  
  181. void
  182. resetView( GLvoid )
  183. {
  184.     distance = near + (far - near) / 2.0;
  185.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  186.     incAngle = 0.0;
  187.     azimAngle = 0.0;
  188. }
  189.  
  190. void
  191. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  192.             GLfloat twist)
  193. {
  194.     glTranslatef( 0.0, 0.0, -distance);
  195.     glRotatef( -twist, 0.0, 0.0, 1.0);
  196.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  197.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  198. }
  199.  
  200. GLvoid
  201. drawScene( GLvoid )
  202. {
  203.     static GLfloat        upperArmColor[] = { 1.0, 0.0, 0.0 };
  204.     static GLfloat        lowerArmColor[] = { 0.8, 0.5, 0.5 };
  205.     
  206.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  207.  
  208.     glPushMatrix();
  209.         polarView( distance, azimAngle, incAngle, twistAngle );
  210.         XYZaxes();
  211.  
  212.         /* Draw the shoulder centered at the new origin */
  213.         glTranslatef( 1.0, 0.0, 0.0 ); 
  214.             glColor3fv( upperArmColor );
  215.         WireBox( beamWidth, beamHeight, beamDepth );
  216.  
  217.         /* Draw the lower arm at the end of the upper arm and 
  218.           * rotate it 45 degrees */
  219.         glTranslatef( 1.0, 0.0, 0.0 );
  220.         glRotatef( 45.0, 0.0, 0.0, 1.0 );
  221.         glTranslatef( 1.0, 0.0, 0.0 );
  222.             glColor3fv( lowerArmColor );
  223.         WireBox( beamWidth, beamHeight, beamDepth );
  224.     glPopMatrix();
  225.  
  226.     glutSwapBuffers();
  227. }
  228.